﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using Grain.Crm.DataMigrator.CrmServices;

namespace Grain.Crm.DataMigrator
{
    public class CrmServiceUtility
    {
        public static CrmService GetCrmService()
        {
            return GetCrmService(null, null, null, null, null);
        }

        public static CrmService GetCrmService(string organizationName)
        {
            return GetCrmService(null, organizationName, null,null,null);
        }

        /// <summary>
        /// Set up the CRM Service.
        /// </summary>
        /// <param name="organizationName">My Organization</param>
        /// <returns>CrmService configured with AD Authentication</returns>
        public static CrmService GetCrmService(string crmServerUrl, string organizationName, string domain, string username, string password)
        {
            // Get the CRM Users appointments
            // Setup the Authentication Token
            CrmAuthenticationToken token = new CrmAuthenticationToken();
            token.OrganizationName = organizationName;
            token.AuthenticationType = 0;
           

            CrmService service = new CrmService();

            if (crmServerUrl != null &&
               crmServerUrl.Length > 0)
            {
                UriBuilder builder = new UriBuilder(crmServerUrl);
                builder.Path = "//MSCRMServices//2007//CrmService.asmx";
                service.Url = builder.Uri.ToString();
            }

            service.Credentials = new NetworkCredential(username, password, domain);  // System.Net.CredentialCache.DefaultCredentials;
            service.CrmAuthenticationTokenValue = token;

            return service;
        }

       

        /// <summary>
        /// Create a Crm label
        /// </summary>
        /// <param name="label">string label value for LocLabel</param>
        /// <param name="langCode">Language Code for CrmLabel</param>
        /// <returns></returns>
        public static CrmLabel CreateSingleLabel(string label, int langCode)
        {
            CrmNumber crmNumber = new CrmNumber();
            crmNumber.Value = langCode;

            LocLabel locLabel = new LocLabel();
            locLabel.LanguageCode = crmNumber;
            locLabel.Label = label;

            CrmLabel crmLabel = new CrmLabel();
            crmLabel.LocLabels = new LocLabel[] { locLabel };

            return crmLabel;
        }
    }


}